package com.cbl.v.ssczs.mode.net;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

public class NetMessage implements Serializable{
    private JSONObject jsonObject = null;
    private String json;



    public NetMessage() {
        jsonObject = new JSONObject();
    }

    public NetMessage(String json) {
        if (json == null) {
            return;
        }
        try {
            this.json = json;
            jsonObject = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public void setJson(String json) {
        try {
            jsonObject = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void setCode(int code) {
        try {
            jsonObject.put(KEY.KEY_STATE, code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void setMsg(String msg) {
        try {
            jsonObject.put(KEY.ATTACH_TEXT_KEY, msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void setData(String data) {
        try {
            jsonObject.put(KEY.RESPONSE_DATA_KEY, data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public int getCode(){
        try {
            return jsonObject.getInt(KEY.CODE_SUCCESS);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 1;
    }


    public boolean getSuccess() {
        try {
            if (jsonObject.getInt(KEY.CODE_SUCCESS)==200) {
                return true;
            } else {
                return false;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public String getMsg() {
        try {
            String message=jsonObject.getString("msg");
            return message;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getData() {
        try {
            return jsonObject.getString(KEY.RESPONSE_DATA_KEY);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getParameter() {
        try {
            return jsonObject.getString(KEY.RESPONSE_DATA_EER_KEY);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getJson() {
        if (jsonObject == null) {
            return null;
        }
        return jsonObject.toString();
    }

    public String getJsonList() {
        if (json == null) {
            return null;
        }
        try {
            return new JSONArray(json).toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getData(String key) {
        if (jsonObject.has(key)) {
            try {
                return jsonObject.getString(key);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public interface KEY {
        /**
         * 附加消息的key
         */
        public final String ATTACH_TEXT_KEY = "attach_text";
        /**
         * 返回数据的Key
         */
        public final String RESPONSE_DATA_KEY = "data";

        /**
         * 返回数据的额外参数的Key
         */
        public final String RESPONSE_DATA_EER_KEY = "parameter";
        /**
         * MapMessage中状态的key
         */
        public final String KEY_STATE = "MyState";

        /**
         * 成功的key
         */
        public final String KEY_SUCCESS = "success";
        /**
         * 成功的code
         */
        public final String CODE_SUCCESS = "code";
    }

    public interface VALUE {
        /**
         * 操作成功
         */
        public final String RESULT_SUCCESS = "Success";
        /**
         * 操作失败
         */
        public final boolean RESULT_FAILURE = false;
    }


}